package vashion.azeroth.api.controller.member;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.context.pojo.PersonalFavoritesItemContext;
import vashion.azeroth.core.personal.pojo.PersonalFavorites;
import vashion.azeroth.core.personal.pojo.PersonalFeed;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalFavoritesService;

@Controller
@RequestMapping("/api/member")
public class FavoriteController<T> extends BaseController {

	@Autowired
	private PersonalFavoritesService<T> personalFavoritesService;

	/**
	 * 添加关注
	 * 
	 * */
	@RequestMapping("addFavorite.htm")
	@ResponseBody
	public ApiResult<Boolean> addFavorite(
			@ModelAttribute("personalFavorites") PersonalFavorites personalFavorites) {
		if (null == personalFavorites.getMemberId()) {
			return ApiResult.newErrorResult(MemberErrorEnum.NO_LOGIN.getErrorCode(),
					"inviteMemberId" + MemberErrorEnum.NO_LOGIN.getErrorMessage());
		}
		ApiResult<Boolean> result = personalFavoritesService.addFavorit(personalFavorites);
		return result;
	}

	/**
	 * 取消关注
	 * 
	 * */

	@RequestMapping("cancelFavorite.htm")
	@ResponseBody
	public ApiResult<Boolean> cancelFavorite(
			@ModelAttribute("personalFavorites") PersonalFavorites personalFavorites) {
		if (null == personalFavorites.getMemberId()) {
			return ApiResult.newErrorResult(MemberErrorEnum.NO_LOGIN.getErrorCode(),
					"inviteMemberId" + MemberErrorEnum.NO_LOGIN.getErrorMessage());
		}
		ApiResult<Boolean> result = personalFavoritesService
				.cancelFavorite(personalFavorites);

		return result;
	}

	/**
	 * 查询我关注的贴子
	 * 
	 * */

	@RequestMapping("queryMyFacoritePost.htm")
	@ResponseBody
	public ApiResult<List<PersonalFeed>> queryMyFacoritePost(
			@ModelAttribute("personalFavorites") PersonalFavorites personalFavorites) {

		Long memberId = personalFavorites.getMemberId();

		if (null == memberId) {
			return ApiResult.newErrorResult(MemberErrorEnum.NO_LOGIN.getErrorCode(),
					"inviteMemberId" + MemberErrorEnum.NO_LOGIN.getErrorMessage());
		}

		int pageNum = personalFavorites.getPageNum();

		int pageSize = personalFavorites.getPageSize();

		int firstRow = (pageNum - 1) < 0 ? 1 : pageNum;

		firstRow = (firstRow - 1) * pageSize;

		ApiResult<List<PersonalFeed>> result = personalFavoritesService
				.queryMyFacoritePost(memberId, firstRow, pageSize);

		return result;
	}

	/**
	 * 查询我关注的商品
	 * 
	 * */

	@RequestMapping("queryFavorityItems.htm")
	@ResponseBody
	public ApiResult<List<PersonalFavoritesItemContext>> queryFavorityItems(
			@ModelAttribute("personalFavorites") PersonalFavorites personalFavorites,
			@RequestParam(value = "curno", required = false) String curno) {

		Long memberId = personalFavorites.getMemberId();

		if (null == memberId) {
			return ApiResult.newErrorResult(MemberErrorEnum.NO_LOGIN.getErrorCode(),
					"inviteMemberId" + MemberErrorEnum.NO_LOGIN.getErrorMessage());
		}

		Integer pageNum = personalFavorites.getPageNum();

		Integer pageSize = personalFavorites.getPageSize();

		pageNum = (pageNum - 1) < 0 ? 1 : pageNum;

		pageNum = (pageNum - 1) * pageSize;

		ApiResult<List<PersonalFavoritesItemContext>> result = personalFavoritesService
				.queryMyFavoriteItem(memberId, pageNum, pageSize, curno);
		return result;
	}

	/**
	 * 查询文章或贴子是否已收藏
	 * 
	 * */

	@RequestMapping("queryIsFavorite.htm")
	@ResponseBody
	public ApiResult<Boolean> queryIsFavorite(
			@ModelAttribute("personalFavorites") PersonalFavorites personalFavorites) {

		Long memberId = personalFavorites.getMemberId();

		if (null == memberId) {
			return ApiResult.newErrorResult(MemberErrorEnum.NO_LOGIN.getErrorCode(),
					"inviteMemberId" + MemberErrorEnum.NO_LOGIN.getErrorMessage());
		}

		Integer type = personalFavorites.getType();

		String targetCode = personalFavorites.getTargetCode();

		//int pageSize = personalFavorites.getPageSize();

		ApiResult<Boolean> result = personalFavoritesService.queryIsFavorited(
				memberId, type, targetCode);
		return result;
	}

	/**
	 * 查询文章或贴子收藏数量
	 * 
	 * */

	@RequestMapping("queryFavoriteCnt.htm")
	@ResponseBody
	public ApiResult<Integer> queryFavoriteCnt(
			@ModelAttribute("personalFavorites") PersonalFavorites personalFavorites) {

		Long memberId = personalFavorites.getMemberId();
		
		if (null == memberId) {
			return ApiResult.newErrorResult(MemberErrorEnum.NO_LOGIN.getErrorCode(),
					"inviteMemberId" + MemberErrorEnum.NO_LOGIN.getErrorMessage());
		}

		Integer type = personalFavorites.getType();

		String targetCode = personalFavorites.getTargetCode();

		//int pageSize = personalFavorites.getPageSize();

		ApiResult<Integer> result = personalFavoritesService.queryFavoriteCnt(memberId,
				type, targetCode);
		return result;
	}
}
